from sqlalchemy import Column, Integer, String, DateTime, Text, JSON
from sqlalchemy.sql import func
from database import Base


class User(Base):
    """用户模型"""
    __tablename__ = "users"

    id = Column(Integer, primary_key=True, index=True)
    username = Column(String(50), unique=True, index=True, nullable=False)
    email = Column(String(100), unique=True, index=True, nullable=False)
    hashed_password = Column(String(255), nullable=False)
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    updated_at = Column(DateTime(timezone=True), onupdate=func.now())


class SystemConfig(Base):
    """系统配置模型"""
    __tablename__ = "system_configs"

    id = Column(Integer, primary_key=True, index=True)
    # 数字人配置
    avatar_prompt = Column(Text, nullable=True, comment="数字人提示词配置")
    
    # 内部知识库配置
    internal_knowledge_id = Column(String(255), nullable=True, comment="内部知识库ID")
    internal_knowledge_api_key = Column(String(255), nullable=True, comment="内部知识库API密钥")
    
    # 外部知识库配置
    external_knowledge_id = Column(String(255), nullable=True, comment="外部知识库ID")
    external_knowledge_api_key = Column(String(255), nullable=True, comment="外部知识库API密钥")
    
    # AI模型配置
    ai_model_api_url = Column(String(255), nullable=True, comment="AI模型API地址")
    ai_model_name = Column(String(100), nullable=True, comment="AI模型名称")
    ai_model_api_key = Column(String(255), nullable=True, comment="AI模型API密钥")
    
    # 音色配置
    voice_config = Column(String(255), nullable=True, comment="音色配置")
    
    # 楼层导览图片路径
    floor_guide_image = Column(String(500), nullable=True, comment="楼层导览图片路径")
    
    # 视频资源配置（JSON格式存储）
    video_resources = Column(JSON, nullable=True, comment="视频资源配置")
    
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    updated_at = Column(DateTime(timezone=True), onupdate=func.now())


class Conversation(Base):
    """对话历史模型"""
    __tablename__ = "conversations"

    id = Column(Integer, primary_key=True, index=True)
    session_id = Column(String(255), unique=True, index=True, nullable=False, comment="会话ID")
    title = Column(String(255), nullable=False, comment="对话标题")
    messages = Column(JSON, nullable=False, default=list, comment="消息列表")
    source = Column(String(20), nullable=True, default="internal", index=True, comment="对话来源: internal-内部聊天, external-外部聊天")
    created_at = Column(DateTime(timezone=True), server_default=func.now(), comment="创建时间")
    updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now(), comment="最近更新时间")


class Question(Base):
    """问题管理模型"""
    __tablename__ = "questions"

    id = Column(Integer, primary_key=True, index=True)
    title = Column(String(500), nullable=False, index=True, comment="问题标题")
    category = Column(String(100), nullable=False, index=True, comment="问题分类")
    status = Column(String(20), nullable=False, default="active", index=True, comment="问题状态：active-启用, inactive-停用")
    usage_count = Column(Integer, nullable=False, default=0, comment="使用次数")
    created_at = Column(DateTime(timezone=True), server_default=func.now(), comment="创建时间")
    updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now(), comment="最近更新时间")
